﻿<%@ Application Language="VB" %>

<script runat="server">


    
    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Codice eseguito all\'avvio dell\'applicazione
        
        MyApp.InitApplication()
        
        
        
    End Sub
    
    
    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Codice eseguito all\'arresto dell\'applicazione
        MyApp.WriteProcLog("End application", MyApp.Task.TaskType.SCHEDULE)
    End Sub
        
    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Codice eseguito in caso di errore non gestito
        Dim logger As NLog.Logger = NLog.LogManager.GetLogger("GlobalError")
        Dim ex = Server.GetLastError
        Dim li = New NLog.LogEventInfo With {.Level = NLog.LogLevel.Fatal, .TimeStamp = Now, .Message = ex.ToString.Replace(vbLf, vbCrLf)}
        li.Properties.Add("url", Request.Url.AbsoluteUri)

        Dim SessionEnabled = HttpContext.Current.Session IsNot Nothing

        ' pagina corrente
        Dim dati = ""

        'If HttpContext.Current.Items("CURRENT_TASK") IsNot Nothing Then
        '    MyApp.sched_update(HttpContext.Current.Items("CURRENT_TASK"), MyApp.Task.TaskStatusType.READY, Now, "ERROR")
        '    dati &= "CURRENT_TASK = " & HttpContext.Current.Items("CURRENT_TASK") & vbCrLf
        'End If

        For Each ccc In Request.Form.Keys
            
            If ccc IsNot Nothing Then
                If ccc <> "__VIEWSTATE" And ccc <> "__EVENTVALIDATION" Then
                    Dim v = Request.Form(ccc).ToString
                    v = v.Replace("$", " --> ")
                    dati &= SF("[{0}] === {1}" & vbCrLf, ccc.Replace("$", " --> "), v)
                
                End If
            End If

        Next
        
        Dim sess = ""
        If SessionEnabled Then
            For Each ccc In Session.Keys
            
                If sess IsNot Nothing Then
                    'If ccc <> "__VIEWSTATE" And ccc <> "__EVENTVALIDATION" Then
                    sess &= SF("[{0}] === {1}" & vbCrLf, ccc, Session(ccc))
                
                    'End If
                End If

            Next
        End If

        Dim data_err = ""
        For Each ccc In ex.InnerException.Data
            data_err &= SF("[{0}] === {1}" & vbCrLf, ccc.key, ccc.value)
        Next

        
        'ueser
        Dim user_data = ""
        If SessionEnabled Then
            Dim curruser = MyApp.GetUser(False)
            If curruser IsNot Nothing Then
                user_data = curruser.Session_Json
            End If
        End If
        
        
        li.Properties.Add("error_data", data_err)
        li.Properties.Add("form", dati)
        li.Properties.Add("session", sess)
        li.Properties.Add("user_data", user_data)
        li.Properties.Add("useragent", Request.UserAgent)
        li.Properties.Add("userhostaddress", Request.UserHostAddress)
        
        '__VIEWSTATE
        
        logger.Log(li)

    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Codice eseguito all\'avvio di una nuova sessione

        MyApp.InitSession()

    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Codice eseguito al termine di una sessione. 
        ' Nota: l\'evento Session_End viene generato solo quando la modalità sessionstate
        ' è impostata su InProc nel file Web.config. Se la modalità è impostata su StateServer 
        ' o SQLServer, l\'evento non viene generato.
    End Sub
       
</script>